package com.lagou.boot;

import com.lagou.handler.UserServiceHandler;
import com.lagou.pojo.RpcRequest;
import com.lagou.serializer.impl.JSONSerializer;
import com.lagou.utils.RpcDecoder;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;
import org.I0Itec.zkclient.ZkClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * @author czq
 * @date 2020/8/10 19:43
 */
@Component
public class ServerBoot implements CommandLineRunner {

    @Autowired
    private UserServiceHandler userServiceHandler;

    public static final String IP = "127.0.0.1";

    public static final Integer PORT = 8999;

    @Override
    public void run(String... args) throws Exception {
        startServer();
    }

    /**
     * 创建一个方法启动服务器
     */
    private void startServer() throws InterruptedException {

        // 1、创建两个线程池对象
        NioEventLoopGroup boosGroup = new NioEventLoopGroup();
        NioEventLoopGroup workGroup = new NioEventLoopGroup();

        try {

            // 2、创建服务端的启动引导对象
            ServerBootstrap serverBootstrap = new ServerBootstrap();

            // 3、配置启动引导对象
            serverBootstrap.group(boosGroup, workGroup)
                    // 设置通道为 Nio
                    .channel(NioServerSocketChannel.class)
                    // 创建监听 channel
                    .childHandler(new ChannelInitializer<NioSocketChannel>() {
                        @Override
                        protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
                            // 获取管道对象
                            ChannelPipeline pipeline = nioSocketChannel.pipeline();
                            // 给管道对象 pipeline 设置编码
                            pipeline.addLast(new StringEncoder());
                            pipeline.addLast(new RpcDecoder(RpcRequest.class, new JSONSerializer()));
                            // 把我们自定的一个 ChannelHandler 添加到通道中
                            pipeline.addLast(userServiceHandler);
                        }
                    });

            ZkClient zkClient = new ZkClient("127.0.0.1:2181");
            String path = "/lg-ZkClient";
            if (!zkClient.exists(path)) {
                zkClient.createPersistent(path);
            }
            // 创建 ip_port 形式的临时节点
            zkClient.createEphemeral(path + "/" + IP + "_" + PORT, "init");

            System.out.println(PORT + " 服务器启动成功");

            // 4、绑定端口
            ChannelFuture channelFuture = serverBootstrap.bind(IP, PORT).sync();

            channelFuture.channel().closeFuture().sync();
        } finally {
            boosGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }
    }
}
